#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::fstream;
using std::string;

void test()
{
    //对于文件输入输出流而言，当文件不存在的时候，就打开失败
    //当文件存在的时候，就可以进行正常的读操作
    fstream fs("fs.txt");
    if(!fs.good())
    {
        std::cerr << "fs is not good" << endl;
        return;
    }

    string line;
    while(getline(fs, line))
    {
        cout << line << "  ";
    }
    cout << endl;

    fs.close();
}

//需求：想从键盘输入五个int类型的数据，然后写到文件中，
//接着将文件中的内容读出来，并且显示到屏幕上
void test2()
{
    fstream fs("fs.txt");
    if(!fs.good())
    {
        std::cerr << "fs is not good" << endl;
        return;
    }

    int number = 0;
    for(size_t idx = 0; idx < 5; ++idx)
    {
        cin >> number;
        fs << number << " ";
    }

    //tellg/tellp获取文件流对象的位置
    //g = get  p = put
    size_t pos = fs.tellg();
    /* fs.tellp();//ok */
    cout << "pos = " << pos << endl;
    //seekp/seekg设置文件流对象的位置
    //g = get  p = put
    /* fs.seekg(0); */
    /* fs.seekg(0, std::ios::beg); */
    fs.seekg(-pos, std::ios::end);

    for(size_t idx = 0; idx < 5; ++idx)
    {
        //调试代码一定要有思路
    /* cout << "fs.goodbit = " << fs.good() << endl; */
    /* cout << "fs.badbit = " << fs.bad() << endl; */
    /* cout << "fs.failbit = " << fs.fail() << endl; */
    /* cout << "fs.eofbit = " << fs.eof() << endl; */
        fs >> number;
        cout << number << " ";
    }
    cout << endl;

    fs.close();

}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

